<?php
/*
@author: wjf
@contact: Hurrican
@software: PhpStorm
@create: 2022/9/20 15:22
*/
declare(strict_types=1);
namespace app\common\service;
use TencentCloud\Sms\V20190711\SmsClient;
// 导入要请求接口对应的 Request 类
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
// 导入可选配置类
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use think\facade\Log;
use think\helper\Str;

class ShortSmsBack extends BaseService {

    protected $SecretId; // 腾讯云账户秘密ID，注意不是短信中的id
    protected $SecretKey; // 腾讯云账户中的秘钥key
    protected $AppID; // 腾讯云短信应用appid。应用管理 > 应用列表中的应用id
    protected $smsSign; // 模板签名内容。一定是要 国内短信 > 签名管理 > 中的签名内容
    protected $templateId;// 正文模板中的模板id。 国内短信 > 正文模板管理 > 模板ID
    public $error ='';
    public function __construct()
    {
        $this->SecretId = env('SMS.sid', 'AKIDRkqhJkwtc5LM6cw1sVt3yd4UfePvDG4c');
        $this->SecretKey = env('SMS.skey', 'QbXDfspX1JMAi15tTq31kESRCj180cz6');
        $this->AppID = env('SMS.appid', '1400739552');
        $this->smsSign = env('SMS.sign', '遇见白泽');
        $this->templateId = '';
    }
    /**
     * 发送验证码
     * * @param string $phone 手机号码
     * * @param int $t_type 模板类型 1 注册 ……
     */
    public function sendcode($phone = '',$t_type = 1){

        $phone ="+86".$phone;
       //设置请求模板
        switch ($t_type){
            case 1:
                $this->templateId = "1549261";
                break;
            default:
                $this->templateId = "1549261";
        }
        try {
            // 实例化认证对象
            $cred = new Credential($this->SecretId, $this->SecretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个 http 选项
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            // 实例化一个 client 选项
            $client = new SmsClient($cred, "", $clientProfile);
            // 实例化一个 sms 发送短信请求对象，每个接口都会对应一个 request 对象。
            $req = new SendSmsRequest();
            // 发送短信验证码配置参数
            //$params = '{"PhoneNumberSet":["'.$phone.'"],"TemplateID":"'.$this->templateId.'", "Sign":"'.$this->smsSign.'","TemplateParamSet":["'.$TemplateParamSet.'"],"SmsSdkAppid":"'.$this->AppID.'"}';
            $params = [
                'PhoneNumberSet' => [
                    $phone,
                ],
                'TemplateID' => $this->templateId,
                'Sign' => $this->smsSign,
                'TemplateParamSet' => [
                    "123456",
                    "2"
                ],
                'SmsSdkAppid' => $this->AppID,
            ];
            $params = json_encode($params,JSON_UNESCAPED_UNICODE);
            $req->fromJsonString($params);
            // 通过 client 对象调用 SendSms 方法发起请求
            $resp = $client->SendSms($req);
            if ($resp->SendStatusSet[0]->Code !='Ok'){
                $this->error ='发送失败';
                return false;
            }
            return true;
        } catch(TencentCloudSDKException $e) {
            Log::info('短信发送失败原因：'.$e);
            return false;
        }
    }

    public function setVcode():string
    {
        $code = Str::random(6);
        return $code;

    }
}
